// include the library code:
#include "../../common/TypesCommands.hpp"
#include "Types.hpp"
#include "CButtonHandler.hpp"

// initialize the library with the numbers of the interface pins
//LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

//const int BUTTON_VALUE_INCORRECT = 255;

CButtonHandler button1(8);

unsigned long lightOnMillis = 0;        // will store last time LED was updated
unsigned long lightTime = 10000;
const int buttonPin = 8;

int commandCount = 0;
int valueCount = 0;

unsigned long sentMillis = 0;
unsigned long sendTime = 5000;
uint8_t previousButtonState = LOW;
uint8_t currentButtonState = LOW;

boolean relayEnabled = false;

void setup(){
    // set up the LCD's number of columns and rows: 
  //lcd.begin(16, 2);
  // initialize the serial communications:
  Serial.begin(9600);
  
  pinMode(7, OUTPUT);
  //pinMode(buttonPin, INPUT);
  button1.init();
  
  pinMode(13, OUTPUT);
  digitalWrite(7, HIGH);
}

void loop()
{
  if ((millis() - lightOnMillis) > lightTime)
  {
      digitalWrite(7, LOW);
  }
  
  if ((millis() - sentMillis) > sendTime)
  {
      sentMillis = millis();
      Serial.write(commandCount);
      Serial.write(valueCount+3);
      Serial.write(valueCount+2);
      Serial.write(valueCount+1);
      Serial.write(valueCount);
      valueCount++;
      commandCount++;
      if (commandCount > 5) commandCount = 0;
  }

  uint8_t btnValue = button1.processButton();
  if (BUTTON_PRESS == btnValue)
  {
   // pressed
   Serial.write(1);
   Serial.write(0);
   Serial.write(0);
   Serial.write(0);
   Serial.write(1);
  } else if (BUTTON_RELEASE == btnValue)
  {
   // released
   Serial.write(1);
   Serial.write(0);
   Serial.write(0);
   Serial.write(0);
   Serial.write(0);
  }
  
  
  // when characters arrive over the serial port...
  if (Serial.available())
  {
    digitalWrite(7, HIGH);
    lightOnMillis = millis();
    
    // wait a bit for the entire message to arrive
    delay(100);
    
    // clear the screen
    //lcd.clear();
    // read all the available characters
    while (Serial.available() > 0) {
      // display each character to the LCD
      //lcd.write(Serial.read());
    }
  }
}
